package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalityType;
import java.awt.Window.Type;
import java.awt.Dialog.ModalExclusionType;

public class ConfigConnectionDialog extends JDialog {

	private JPanel contentPane;
	private JTextField textFieldIp;
	private JTextField textFieldPort;
	private MainScreen parentComponent;
	private JButton btnConnectToServer;
	public ConfigConnectionDialog(MainScreen parentComponent)
	{	
		super(parentComponent);
		InitComponent();
		this.parentComponent=parentComponent;		
	}
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					ConfigConnectionDialog frame = new ConfigConnectionDialog();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public ConfigConnectionDialog() {
		setResizable(false);
		InitComponent();
	}

	private void InitComponent() {
		setTitle("Config Connection");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 326, 211);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		textFieldIp = new JTextField();
		textFieldIp.setText("127.0.0.1");
		textFieldIp.setColumns(10);
		
		textFieldPort = new JTextField();
		textFieldPort.setText("3200");
		textFieldPort.setColumns(10);
		
		JLabel lblUsername = new JLabel("Ip:");
		
		JLabel lblIp = new JLabel("Port:");
		
		btnConnectToServer = new JButton("Connect To Server");
		btnConnectToServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnConnectToServer_actionPerformed();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(25, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnConnectToServer)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblUsername)
								.addComponent(lblIp, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
							.addGap(28)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(textFieldPort, GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
								.addComponent(textFieldIp))))
					.addGap(50))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(33)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblUsername)
						.addComponent(textFieldIp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIp)
						.addComponent(textFieldPort, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(btnConnectToServer)
					.addContainerGap(61, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	protected void btnConnectToServer_actionPerformed() {
		try{
			this.parentComponent.SetIP(this.textFieldIp.getText());
			this.parentComponent.SetPort(Integer.parseInt(this.textFieldPort.getText()));
			this.parentComponent.connect();
			this.setVisible(false);
			this.dispose();
		}
		catch (Exception e) {

		}
		
		
	}
}
